<?php

namespace app\asmpasync\service;
use think\Db;

/**
 * @name: 函谷盾
 * @Param: 
 * @Description: 
 * @Author: foo
 * @Date: 2019-11-26 15:45:41
 */

class Hgshield
{
    public $classDescription = '函谷盾服务';//模块描述
    public $actionDescription = '';//操作描述
    /**
     * @name: 单个规则组装
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-26 15:08:46
     */
    private function rule ($ruleData)
    {
        $return = [];
        foreach ($ruleData as $v)
        {
            $Rule = new \Hgshield\Rule();
            $Rule->setRule($v['rule']);
            $Rule->setMode($v['model']);
            $return[] = $Rule;
        }

        return $return;
    }
    
    /**
     * @name: 函谷盾 规则下发
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-26 15:02:27
     */
    public function rules ($host_id)
    {
        $this->actionDescription = '规则下发';
        $ipsData = Db::name('IpsRule')->field('execute_content rule, handle model')->where('host_id', $host_id)->where('status', 1)->select();
        $normalData = Db::name('NormalRule')->alias('nr')->field('nr.content rule, hnrr.handle model')->join('HostNormalRuleRelation hnrr', 'nr.id = hnrr.rule_id')->where('hnrr.host_id', $host_id)->where('hnrr.status', 1)->select();
        $data = array_merge($ipsData, $normalData);
        $data = array_map(function ($v){
            return [
                'rule' => $v['rule'],
                'model' => $v['model'] > 0 ? $v['model'] > 1 ? 'alert' : 'pass' : 'reject' ,
            ];
        }, $data);
        $ruleData = $this->rule($data);
        $Rules = new \Hgshield\WipfRules();
        $Rules->setWipfrules($ruleData);
        
        return $Rules;
    }

    /**
     * @name: 开启监控
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-26 15:03:44
     */
    public function onOriginalRule ($host_id, $data)
    {
        $this->actionDescription = '开启监控';
        $data = array_map(function ($v) {
            return [
                'rule' => $v['rule'],
                'model' => '1' ,
            ];
        },$data);

        $ruleData = $this->rule($data);
        $Iptables = new \Hgshield\Iptables();
        $Iptables->setIptables($ruleData);

        return $Iptables;
    }

        /**
     * @name: 关闭监控
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-26 15:03:44
     */
    public function offOriginalRule ($host_id, $data)
    {
        $this->actionDescription = '关闭监控';
        $data = array_map(function ($v) {
            return [
                'rule' => $v['rule'],
                'model' => '0' ,
            ];
        },$data);

        $ruleData = $this->rule($data);
        $Iptables = new \Hgshield\Iptables();
        $Iptables->setIptables($ruleData);

        return $Iptables;
    }

    /**
     * @name: 删除规则
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-13 10:32:35
     */
    public function delOriginalRule ($host_id, $data)
    {
        $this->actionDescription = '删除规则';
        $data = array_map(function ($v) {
            return [
                'rule' => $v['rule'],
                'model' => '3' ,
            ];
        }, $data);
        
        $ruleData = $this->rule($data);
        $Iptables = new \Hgshield\Iptables();
        $Iptables->setIptables($ruleData);

        return $Iptables;
    }

    /**
     * @name: 函谷盾配置
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-26 15:04:18
     */
    public function config ($host_id)
    {
        $this->actionDescription = '配置修改';
        $ipsConfig = Db::name('IpsConfig')->where('host_id', $host_id)->find();

        $Config = new \Hgshield\Config();
        $Config->setWipfMode($ipsConfig['type'] == 1 ? 'ips' : 'ids');
        $Config->setIpsProcessStatus($ipsConfig['status'] ? true : false);
        $Config->setIdsDevice($ipsConfig['device_ip']);
        $Config->setIpsDevice($ipsConfig['protect_ip']);
        $Config->setIpsBlackList($ipsConfig['black'] ? true : false);
        $Config->setLock($ipsConfig['firewall'] ? true : false);

        return $Config;
    }

    /**
     * @name: 清空黑名单
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-26 17:24:28
     */
    public function clearBlackList ()
    {
        $this->actionDescription = '清空黑名单';
        $Webcmd = new \Hgshield\Webcmd();
        $Webcmd->setCmd('ClearBlackList');

        return $Webcmd;
    }

    /**
     * @name: 转化规则上传
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-26 17:24:28
     */
    public function convertRule ()
    {
        $this->actionDescription = '转化规则上传';
        $Webcmd = new \Hgshield\Webcmd();
        $Webcmd->setCmd('ConvertRule');

        return $Webcmd;
    }
}